package org.xtext.runtime.term;

import java.util.Map;

import org.xtext.runtime.formula.Predicate;
import org.xtext.runtime.type.IType;
import org.xtext.runtime.type.TypeException;
import org.xtext.runtime.util.Bindings;
import org.xtext.runtime.util.ILogicVisitor;


public class LibraryTerm implements ITerm {
	private IType type;
	private String identifier;
	private Predicate function;
	
	public LibraryTerm(String identifier, IType type, Predicate function) throws TypeException {
		this.identifier = identifier;
		this.function = function;
		this.type = type;
//		System.out.println("setting type: " + type);
	}
	
	@Override
	public IType type() {
		return type;
	}

	@Override
	public ITerm copy() {
		try {
			return new LibraryTerm(identifier, type, function.copy());
		} catch (TypeException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}

	@Override
	public Object value() {
		throw new UnsupportedOperationException();
	}

	public String toString() {
		return identifier + "." + function;
	}

	@Override
	public LibraryTerm copy(Map<String, Variable> vars) {
		try {
			return new LibraryTerm(identifier, type, function.copy(vars));
		} catch (TypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public LibraryTerm apply(Bindings bindings) {
		function.apply(bindings);
		return this;
	}


	@Override
	public boolean hasFreeVariables() {
		return function.hasFreeVariables();
	}

	@Override
	public Object accept(ILogicVisitor visitor) {
		return visitor.visit(this);
	}

	public String identifier() {
		return identifier;
	}

	public Predicate function() {
		return function;
	}
}
